package com.hankol.material

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseActivity
import com.baselibrary.manager.LoadingManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.hankol.material.api.MaterialConstants
import com.hankol.material.bean.FolderListBean
import com.hankol.material.databinding.ActivityCreateFolderBinding
import com.hankol.material.help.MaterialHelp
import com.hankol.material.view.LimitInputTextWatcher
import com.hankol.material.viewmodel.MaterialCreateFolderViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode



/**
 * @author: 关忆北丶
 * @time: 2021/10/25 19:32
 * @description: 创建文件夹
 */
@Route(path = ActivityRouter.MATERIAL.MATERIAL_CREATE_FOLDER)
class CreateFolderActivity : BaseActivity() {
    private lateinit var dataBinding: ActivityCreateFolderBinding
    private lateinit var mViewModel: MaterialCreateFolderViewModel
    private var mFolderListBean: FolderListBean? = null//重命名用到
    private var folderContent: String = ""
    private var isCreate: Boolean = true//是否是创建文件夹  true 创建文件夹  false重命名
    override fun getLayoutId(): Int {
        return R.layout.activity_create_folder
    }

    override fun init() {
        StatusBarUtil.setTranslucentStatus(this)
        dataBinding = getDataBinding<ActivityCreateFolderBinding>()
        mViewModel = getActivityViewModelProvider(this).get(MaterialCreateFolderViewModel::class.java)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        bundle?.apply {
            if (containsKey(MaterialConstants.FOLDER_CONTENT)) {
                mFolderListBean = getSerializable(MaterialConstants.FOLDER_CONTENT) as FolderListBean
                mFolderListBean?.let {
                    isCreate = false
                    dataBinding.edtCreateFolder.setText(it.name)
                }
            }
        }
        initListener()
        if (isCreate) {
            mViewModel.createResultBean.observe(this, Observer {
                LoadingManager.hideLoadingDialog(this)
                if (it == null) return@Observer
                EventBusUtils.sendJavaBean(MessageBus.msgId_material_createFolder, mViewModel.dataChanage(it))
                ToastUtils.showShort(applicationContext, "添加成功")
                finish()
            })
        } else {
            mViewModel.renameFolderResultBean.observe(this, Observer {
                LoadingManager.hideLoadingDialog(this)
                if (!it) return@Observer
                mFolderListBean?.let {
                    it.name = folderContent
                    EventBusUtils.sendJavaBean(MessageBus.msgId_material_upFolder, it)
                    finish()
                }
            })
        }

    }


    private fun initListener() {
        //dataBinding.edtCreateFolder.filters = arrayOf<InputFilter>(mViewModel.CreateFolderFilter())
        dataBinding.edtCreateFolder.addTextChangedListener(LimitInputTextWatcher(dataBinding.edtCreateFolder));
        dataBinding.llCreateFolderBack.setOnClickListener { finish() }
        dataBinding.tvCreateFolderComplete.setOnClickListener {
            if (!HKUtils.isFastClick()) return@setOnClickListener
            folderContent = dataBinding.edtCreateFolder.text.toString().trim()
            if (folderContent.isEmpty()) {
                ToastUtils.showShort(applicationContext, "请输入文件夹名称")
                return@setOnClickListener
            }

            KeyBoardUtils.hideSoftKeyboard(this)

            if (isCreate) {
                LoadingManager.showLoadingDialog(this)
                mViewModel.createFolder(folderContent)
            } else {
                mFolderListBean?.let {
                    LoadingManager.showLoadingDialog(this)
                    mViewModel.renameFolder(folderContent, it.id)
                }
            }
        }

/*
        dataBinding.edtCreateFolder.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence, start: Int, before: Int,
                                       count: Int) {
                if (s.toString().contains(" ")) {
                    val str = s.toString().split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                    var str1 = ""
                    for (i in str.indices) {
                        str1 += str[i]
                    }
                    dataBinding.edtCreateFolder.setText(str1)
                    dataBinding.edtCreateFolder.setSelection(str1.length)
                }
            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int,
                                           after: Int) {
            }

            override fun afterTextChanged(s: Editable) {}
        })
*/


    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }

    }
}
